package shooting.ui.missilepattern
{
	import flash.geom.Point;
	
	import shooting.ui.MissileContainer;
	import shooting.ui.missile.IMissile;
	import shooting.ui.missile.MissaleBase;
	import shooting.ui.missilepattern.string.StrPattern;
	import shooting.ui.missilepattern.string.StrPatternSet0;
	
	public class MissalePatternString implements IMissilePattern
	{
		private var objectContainer:MissileContainer;
		private var strPattern:StrPattern;
		private var _direction:Number;
		private var _kind:String;
		private var _angleRange:Number;
		private var _count:uint;
		private var _speed:Number;
		private var _interval:Number;
		private var _accSpeed:Number;
		private var _accAngle:Number;
		private var _position:Point;
		private var _renderCount:uint;
		private var stringLineCount:uint;
		private var temStrArr:Array;
		
		private var temAngle:Number;
		private var temDirection:Number;
		
		public function MissalePatternString(kind:String, position:Point, direction:Number, speed:Number = 1, interval:uint = 30, angleRange:Number = 60, strArr:Array = null)
		{
			strPattern = new StrPattern();
			
			_direction = direction;
			_kind = kind;
			_angleRange = angleRange;
			_speed = speed;
			_interval = interval;
			_position = position;
			
			if(strArr == null)
			{
				strArr = StrPatternSet0.HELLO;
			}
			
			strPattern.setData(strArr);
			temStrArr = null;
			
			_count = strPattern.length;
			
			temAngle = _angleRange / _count;
			temDirection = _direction + (temAngle/2) - (_angleRange/2)+ 90;
			temAngle = temAngle / 180 * Math.PI;
			temDirection = temDirection  / 180 * Math.PI;
			
			_renderCount = 0;
			stringLineCount = 0;
		}

		public function get angle():Number
		{
			return _direction;
		}
		
		public function set angle(r:Number):void
		{
			_direction = r;
		}
		
		public function setContainer(container:MissileContainer):void
		{
			objectContainer = container;
		}
		
		public function setString(strPattern:Array):void
		{
			temStrArr = strPattern;
		}
		
		public function render():void
		{
			_renderCount++;
			
			if( (_renderCount % _interval) == 0)
			{
				var lineString:String = strPattern.data[strPattern.data.length - 1 - stringLineCount];
				var i:uint;
				for(i = 0; i < _count; i++)
				{
					if(lineString.substr(_count - 1 - i,1) == "#")
					{
						var speed:Number = _speed;
						var direct:Number = temDirection + (temAngle * i);
						var ms:IMissile = new MissaleBase(_kind, _position.clone(), speed, direct);
						objectContainer.addMissile(ms);
					}
				}
				
				
				stringLineCount++;
				if(stringLineCount >= strPattern.data.length && temStrArr != null)
				{
					strPattern.setData(temStrArr);
					temStrArr = null;
					_count = strPattern.length;
					
					temAngle = _angleRange / _count;
					temDirection = _direction + (temAngle/2) - (_angleRange/2)+ 90;
					temAngle = temAngle / 180 * Math.PI;
					temDirection = temDirection  / 180 * Math.PI;
					
					stringLineCount = 0;
				}
				stringLineCount %= strPattern.data.length;
			}
		}
		
	}
}